package com.syf.kit.soa.core.balance.spi;

import com.syf.kit.soa.core.SOARegionInfo;
import com.syf.kit.soa.core.balance.SyfRpcBalance;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class RoundRpcBalance implements SyfRpcBalance {
    private AtomicInteger seq = new AtomicInteger(0);

    /**
     * 模数
     */
    private int modeNum = 1;

    @Override
    public SOARegionInfo balance(List<SOARegionInfo> soaRegionInfos) {
        if (modeNum != soaRegionInfos.size()) {
            // 说明注册信息变化了，要重新计数
            seq = new AtomicInteger(0);
            modeNum = soaRegionInfos.size();
        }

        return soaRegionInfos.get(seq.incrementAndGet() % modeNum);
    }
}
